-- MathPad is a general purpose graphing scientific calculator. It uses text input rather than simulating buttons on a hand held calculator. This live scratchpad interface allows you to see and edit your entire calculation.
-- This file lists the basic capabilities of MathPad mainly by giving short examples. This entire file can be evaluated by MathPad but it is probably more convenient to open a new window, copy a single example at at time and experiment with it.
-- The "Examples" folder included with this distribution contains some longer examples and utility functions. These examples show how to implement such things as equation solving, curve fitting, vector calculations and numerical solution of differential equations.
---------- Using MathPad as a calculator
-- MathPad may be used as a calculator simply by typing in numbers and operators. When the ENTER key is hit, each line is evaluated and the results are inserted into the text.
2*(3+4):14.0
-- MathPad inserts the ":" and the result "14.0". A status line at the bottom of the window will show a "√" to indicate that evaluation is complete. If any problems are encountered, they will be reported in the status line and the cursor will be moved to the problem in the text. You can edit and re-evaluate the text at any time.
-- The RETURN key simply terminates a line. It does not cause any evaluation. The ENTER key is on the numeric keypad (or next to the space bar on powerbook keyboards).
-- An expression will be continued if the line ends with an operator
2 *
(3+4):14.0
-- More than one expression can be placed on a line if they are separated by semicolons.
2*3+4:10.0; 2.1*(.3+4.2):9.4
-- The character # may be used to reference the last result
10*234:2340.0
#+5:2345.0
-- Numbers may be entered in "e" format as in "12.3e-4". The following letter suffixes may also be used: G for e9, M for e6, k for e3, m for e-3, u or µ for e-6, n for e-9, p for e-12.
-- Hex numbers must be preceded by 0x as in 0xAF23. Hex input is treated as a bit pattern for a 32-bit signed integer.
-- The constant π can be entered via the menu or by typing option-p or "pi".
-- Numeric output format can be controlled via the menu.
------------ Comments
-- MathPad ignores text between "--" and the next RETURN. This file is made up mostly of comments.
-- All text between ~ characters is ignored and can span multiple lines.
-- A line beginning with --¶ will cause a page break on printout (¶ is option 7).
------------ Equations
-- MathPad allows the user to type in equations using variables that will be given numeric values elsewhere in the text.
force=mass*accel
force:160.0
mass=5
accel=32
-- MathPad evaluates the entire text so numeric values can be given before or after they are used.
-- When MathPad cannot calculate a numeric value from the information given, it will print '?' and the name of the variable whose value is unknown.
a=b+c
d=a*4
-- d:? b?
-- MathPad does not perform algebra. The equation x=y+z (or y+z=x) tells how to compute x given y and z but MathPad does not derive any information about how to compute values for y or z from x.
---------- Functions
-- MathPad has most of the usual built-in functions: abs() acos() asin() atan() cos() exp() ln() log() sin() sqrt() tan(). Built-in functions may either be typed in or selected from the menu.
-- The argument units for trig functions can be changed to either degrees or radians with the menu.
-- The built-in function trunc(n) returns the integer part of n.
-- The built-in function sum(expr,var,lower,upper) performs a summation of "expr" while incrementing "var" between "lower" and "upper". The expression given as the first parameter is reevaluated for each step. Expressions for "lower" and "upper" are evaluated only once.
-- The built-in function rand(n) returns a random number in the range 0 to n. More than 10^9 different values are possible. A rand(0) call restarts the sequence.
-- The functions count(),det(),read() and write() operate on arrays and are described in a later section.
-- Users can also define their own functions. Functions differ from equations in that their parameter names refer to temporary variables that can take on different values each time the function is called.
Force(mass,accel)=mass*accel
Force(10,12):120.0
Force(6,8):48.0
mass*accel:160.0; -- uses global values from previous example
-- In this example the parameters "accel" and "mass" are separate from the global variables "accel" and "mass". Any variables used in the definition that do not appear in the parameter list will refer to global variables.
-- Functions can be referenced before they are defined.
-- All references to a function must have the same number of parameters.
-- Multiple definitions of function names are not allowed.
-- MathPad's built-in function menu can be extended by placing "XFun" files in it's folder. See the file "XFun.doc" for more details.
----------- Conditionals
-- The value of an expression can be made dependent on a condition.
p = -a when a < 0,
1 when a = 0,
a^2 otherwise
-- The result will be the value corresponding to first condition in the list that evaluates true. Commas separate expressions in the list.
-- The keyword "otherwise" is optional and is used only to help readability.
-- The conditional follows the keyword "when" and may use the following operators: = != > < >= <= "and" "or" "not" "known" "unknown". The option key versions of comparison operators are also accepted ≠, ≥, ≤. Note that in this context "=" is a comparison operator. The C style operator "==" is also accepted.
-- The prefix operators "known" and "unknown" can be used to test if a numeric value can be calculated for the given expression. Note that a expression can be 'known' even if its value is NAN04 (not a number). NAN04 results from calculations such as 0/0.
-- Parenthesis are used only for arithmetic expressions and can not be used to group conditional operators. Arbitrarily complex conditionals can still be built by using multiple clauses and nested conditionals.
-- Comma separated expressions can also be used without conditionals. The result will be the first item in the list that evaluates to a numeric value.
-------- Recursion
-- MathPad allows recursive function definitions. Depth of recursion is limited by available memory space. If memory runs low during evaluation, recursion is stopped.
-- Evaluation can also be stopped by typing Command-period.
fact(n) = 1 when n < 2, fact(n-1)*n when n > 0
fact(50):3.0e+64
--------- Plots
-- MathPad can produce simple plots by using the "plot" command.
Xmin=0; Xmax=20
Ymin=0; Ymax=Xmax^2
plot X^2
-- Plotting is controlled by special variables. The independent variable "X" is stepped between "Xmin" and "Xmax" with a resolution of "Xsteps" (default Xsteps=100). Other variables Xdiv, Xlabel, Ymin, Ymax, Ydiv, Ylabel, Title, Ystrips can be used to control the plot parameters.
-- Evaluation can be stopped by typing Command-period.
-- The command "newaxis" allows multiple plots/page.
-- The plot and plotline commands can also be used to plot arrays.
-- See the file "Plotting" for more details.
---------- Tables
-- MathPad can produce tables of numeric results using the "table" command.
-- Multiple column tables can be produced by giving a list of expressions separated by commas.
Nmin=1; Nmax=5; Nsteps=5
table N, N^2, N^3
1.0 1.0 1.0
2.0 4.0 8.0
3.0 9.0 27.0
4.0 16.0 64.0
5.0 25.0 125.0
-- Tables are handled in the same way as plots except that the results are inserted as columns in the text window. For tables the independent variable is "N" and the control variables are "Nmin", "Nmax" and "Nsteps".
-- Nsteps is limited to 1000 to prevent accidents from inserting huge amounts of text. Use the write() function for large tables.
-- Do not add any text to the output columns because the table command will not be able to properly delete the table before reevaluating.
-- If table is used with a single expression that is a 1D or 2D array, the array elements will be printed out. The values of Nmin,Nmax and Nsteps are ignored.
------------------- Arrays -------------
A={10,20,30} -- Defines a 3 element array.
A[2]:20.0; -- Accesses the 2nd element. Index values start at 1.
B[1,2]:20.0; -- Both forms of indexing are allowed
A[2:3]:{20.0,30.0}; -- [lo:hi] selects a sub array.
A[2:3][2:2]:{30.0}; -- consecutive selections operate on the same dimension
A[:2]:{10.0,20.0}; -- lo and/or hi may be omitted.
B[2:]:{{40.0,50.0,60.0},{11.0,21.0,31.0}}
Q[i]=i*11 --Arrays can be defined in terms of their index values.
Q[2]:22.0
--dim[] is used to set the number of elements for an array definition. If the expression for number of elements is unknown or < 0, that dimension becomes infinite. dim[0] is equivalent to dim[1].
I[i,j] = 1 when i=j,0 otherwise dim[3,3]
I:{{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}}
--Arrays may be used freely in expressions. Operations are performed on each element. Scalars are extended to contain as many elements as needed.
A+Q:{21.0,42.0,63.0}
2+A:{12.0,22.0,32.0}
log(A):{1.0,1.3,1.5}
A*Q:{110.0,440.0,990.0}; --Note: this is not matrix multiply
-- Q[A]:? A[]?; --Arrays may not be used as index values.
-- For large arrays only the first few elements in each dimension are printed followed by ... to indicate that there are more elements.
Q:{11.0,22.0,33.0,...}
--Use table to output medium sized 1D or 2D arrays.
count(A):3.0; -- count() returns the number of array elements
count(B):3.0; -- elements in 1st dimension
count(42):0.0; -- scalar
count(Q):?; -- infinite array
det(B):580.0; -- calculates the determinant of a square matrix
--------- Data Files
-- read("filename") returns an array of values read from the named data file. Double quotes are needed if the filename contains blanks or punctuation. The data file is assumed to be in the same folder as the source document. Mac style pathnames such as "Hard Disk:DataFolder:file47" are allowed.
-- write("filename",array) writes the elements of the 1D or 2D array to the named file.
-- See "Datafiles" for more information.
--------- Image display
-- The "image" command can be used to display a 2D array. The entire array is displayed and fills the plot area regardless of the values of Xmin, Xmax, Ymin, or Ymax. Scale is controlled by special variables "Zmin" and "Zmax". If Zmin and/or Zmax are not specified MathPad will use Zlo and/or Zhi from the previous evaluation. Display type is controlled by the Display... menu item. For more information see the file "Images"
arr[i,j] = i+j dim[10,10]
Zmin=2; Zmax=20
image arr
--------- Assignments
-- The assignment operator := evaluates the right hand side expression and replaces any previous definition of the left hand variable with the result. Accessing an assigned variable gives its current value and does not reevaluate the expression. Assigned variables do not necessarily have the same value everywhere in the text. This differs from = which gives a single definition of how to calculate the value of a variable.
aa:=3:
aa:3.0
aa:=aa+2:
aa:5.0
aa:={1,2,aa}:
aa:{1.0,2.0,5.0}
aa[2]:=7.6:
aa:{1.0,7.6,5.0}
-- Assignments can only be done to simple global variables.
-- Entire arrays may be assigned (if they are finite).
-- Assignments to array elements are permitted in restricted cases. A single value can be assigned to an element of an array that has previously been initialized by assignment.
-- The result of the := operation itself is always unknown. If you wish to access the value you must use the variable name. This is done so that multiple assignments can be performed using comma separated lists.
-- In cases involving assignments it is sometimes desirable to have evaluation of a sequence continue even if a true "when" clause is encountered. In these cases a double comma can be used to specify that evaluation should continue regardless of the previous clause.
aa when 1=1,42:{1.0,7.6,5.0}
zz when 1=1,,42:42.0
---------- Iteration
-- The 'while' operator can be used to evaluate an expression repeatedly. The condition is checked before each evaluation. Note: i and k are global.
factorial(n) = i:=1,m:=1,(i:=i+1,m:=m*i) while i<n, m
factorial(400):6.4e+868
-- See the file "Programming" for more information on assignments and iteration.
---------- Include files
--Definitions in one file can be shared by other files. A document containing the command include "pathname" will open and read definitions from the specified file. The folder of the source document will be searched first and then the folder containing the MathPad application .Full or relative path names are allowed. You may want to make a folder containing your favorite include files and put it in MathPad's folder. Then any document can access them with a short relative path name.
--A window is opened for each included file. This file can be viewed and edited like any other MathPad document. Any changes made will be seen by the parent document when the parent is reevaluated. Include files should contain only definitions. They can contain other include commands.
---------- Externally compiled functions
--MathPad XFun files placed in the same folder as the application will be added to the function menu. This allows programmers to add functions written in C and compiled as code resources. Items can also be added to MathPad's Help menu. See the folder "XFuns" for more information.
---------- Technical Details
-- Memory size
-- Use the Finder's Get Info... box to set MathPad's memory size depending on your use:
-- 150K if you never use large files, image, read() or write().
-- 170K if you use advanced features and large files.
-- 200K if you use advanced features, large files and large arrays.
-- >200K if you run out of memory when set at 200K. Huge arrays?
-- Data files are read into temporary memory. Large data files can be read if there is enough system memory available. Increasing MathPad's size will not allow larger data files but quitting other applications will.
-- Recursive functions can require large amounts of memory but increasing the MathPad's size past a certain point will not allow deeper recursion. This is because evaluation of recursive functions also uses up stack space which has a fixed allocation size.
-- If you want MathPad to act like a desk accessory, turn on the "Save and Quit" option via the Options... menu and save a document in the Apple Menu Items folder. For documents saved with this option on, clicking the window's close box will save the current text and quit the application. This is similar to the way desk accessory calculators operate. The option affects only the action of the close box. If you choose Close from the File menu, the document will be closed but the application will stay active.
-- If you want MathPad's close box to act like most applications, leave this option off.
-- MathPad checks in its application folder for a file named "defaults". If one is found it is used as stationery for all new documents. This allows setting radians, numeric format, fonts window sizes etc. Create a MathPad document with whatever you want for defaults. (You will have to evaluate a plot to set plot window options). Normally you would delete all the text but default text is allowed. Save the file as "defaults" in MathPad's folder. If you prefer, you can use system 7 Finder to set the file's stationery flag. MathPad doesn't care but it might remind you of its special status.
-- File format
-- MathPad documents are generic TEXT files. Option and plot label information is stored in the resource fork. TEXT files created by other applications may be opened by MathPad and vice versa. The resource fork is not modified until the file is saved. Documents are limited to a maximum size of 30K bytes. Data files can be larger than 30K. See "Datafiles".
-- Background computation
-- Clicking in another application's window will switch even when MathPad is busy with a computation. MathPad will continue computation in the background. The plot window is hidden during background computation and is redrawn when the computation is complete.
-- Background switching is disabled under MultiFinder if any read() operations are done. This restriction does not apply under system 7.
-- Incremental evaluation
--MathPad normally evaluates the entire document when the enter key is pressed. This allows you to edit anything anywhere in the text and have everything updated properly. It tries to take advantage of the fact that usually when text is added at the very end of the document no changes are needed in the part of the document that has already been evaluated.
--MathPad will hold off on reevaluating text that has been successfully evaluated. This can be handy if you wish to examine a variable without having to reevaluate an entire plot or image. Any edit other than at the very end of the document will cause the entire document to be reevaluated on the next enter.
--If you wish to force reevaluation you can use command R instead of enter. One reason for this might be if you plot something using auto ranging and then later add specifications for Xmin etc. Also, if you hit enter without changing anything in the file it will reevaluate.
--------- Distribution
-- MathPad is copyright 1993-1995 by Mark Widholm. It is free for non-commercial distribution. Selling it for profit without my permission is forbidden.
-- If you use MathPad, send me some e-mail. I can notify you of any known bugs, updates etc.
-- Send comments, questions, suggestions and bug reports to:
-- Mark.Widholm@UNH.edu
-- Let me know if you are interested in seeing more examples or if you have a MathPad example that others might be interested in seeing.